package com.xzx.car_accessories.dao.impl;

import com.xzx.car_accessories.dao.InventoryDao;
import com.xzx.car_accessories.dao.utilDao;
import com.xzx.car_accessories.domain.CarAccessory;
import com.xzx.car_accessories.domain.Inventory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: geeshaw
 * @date: 2021/7/13 23:54
 * @email: shaw@bupt.edu.cn
 * @desc:
 **/

@Repository
public class InventoryDaoImpl implements InventoryDao {

    @Autowired
    private final JdbcTemplate jdbcTemplate;

    public InventoryDaoImpl(JdbcTemplate jdbcTemplate)
    {
        this.jdbcTemplate = jdbcTemplate;
    }

    public int inbound(long cid, int num) {
        Map<String, Object> map = new HashMap<>();
        map.put("cid", cid);
        List<Inventory> inventoryList = this.get_diff_inventory(map);
        if(inventoryList.size()==0){
            return 0;
        }
        Inventory inventory =inventoryList.get(0);
        int num_inventory_current = inventory.getNum_inventory();
        int num_inbound_current = inventory.getNum_inbound();
        String update_time = utilDao.get_current_date();
        String sql = "UPDATE `test`.`inventory`" +
                " SET num_inventory = ?, num_inbound = ?, time_latest_inbound=? " +
                " WHERE cid = ?";
        return jdbcTemplate.update(sql, num_inventory_current+num, num_inbound_current+num, update_time, cid);
    }

    public int outbound(long cid, int num) {
        Map<String, Object> map = new HashMap<>();
        map.put("cid", cid);
        List<Inventory> inventoryList = this.get_diff_inventory(map);
        if(inventoryList.size()==0){
            return 0;
        }
        Inventory iventory =inventoryList.get(0);
        int num_inventory_current = iventory.getNum_inventory();
        int num_outbound_current = iventory.getNum_outbound();
        if (num_inventory_current - num < 0){
            System.out.println("库存不足，出库失败");
            return 0;
        }
        String update_time = utilDao.get_current_date();
        String sql = "UPDATE `test`.`inventory`" +
                " SET num_inventory = ?, num_outbound = ?, time_latest_outbound=? " +
                " WHERE cid = ?";

        return jdbcTemplate.update(sql, num_inventory_current-num, num_outbound_current+num, update_time, cid);
    }

    public int init_inventory(long cid) {
        String sql = "SELECT *  FROM `test`.`inventory` WHERE cid = ?";

        List<Inventory> res = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Inventory.class), cid);

        if (res.size() >= 0)
        {
            return 0;
        }

        sql = "INSERT INTO `test`.`inventory` " +
                " (`cid`, `create_time`) VALUES" +
                " (?, ?)";
        String create_time = utilDao.get_current_date();
        return jdbcTemplate.update(sql, cid, create_time);
    }

    public List<Inventory> get_diff_inventory(Map<String, Object> map){
        String sql = "SELECT * FROM `test`.`inventory` WHERE 1 = 1";
        List<Object> argsList = new ArrayList<>();
        if (map!=null){
            if (map.get("cid") != null){
                argsList.add(Long.parseLong(String.valueOf(map.get("cid"))));
                sql += " and cid = ?";
            }

            if (map.get("num_inventory") != null){
                argsList.add(Integer.parseInt(String.valueOf(map.get("num_inventory"))));
                sql += " and num_inventory = ?";
            }

            if (map.get("num_inbound") != null){
                argsList.add(Integer.parseInt(String.valueOf(map.get("num_inbound"))));
                sql += " and num_inbound = ?";
            }

            if (map.get("num_outbound") != null){
                argsList.add(Integer.parseInt(String.valueOf(map.get("num_outbound"))));
                sql += " and num_outbound = ?";
            }

        }
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Inventory.class), argsList.toArray());

    }

    @Override
    public List<Inventory> search_findpage_accessory(int pageNum, int pageSize, String searchWord)
    {
        String sql = " SELECT i.id, c.cid AS cid, c.cname, c.category, c.manufacturer, i.num_inventory, i.num_inbound , " +
                " i.num_outbound, i.time_latest_inbound, i.time_latest_outbound  " +
                " FROM `test`.`car_accessory` AS c , `test`.`inventory` AS i WHERE c.cid = i.cid";

        if(searchWord != "")
        {
            sql += " AND (";

            sql += " c.cid LIKE '%" + searchWord + "%'";

            sql += " or cname LIKE '%" + searchWord + "%'";

            sql += " or category LIKE '%" + searchWord + "%'";

            sql += " or manufacturer LIKE '%" + searchWord + "%' )";
        }

        sql += (" LIMIT "  + (pageNum-1) * pageSize + " , " + pageSize);

        System.out.println(sql);

        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Inventory.class));
    }

    public List<Inventory> search_accessory(String searchWord)
    {
        String sql = " SELECT i.id, c.cid AS cid, c.cname, c.category, c.manufacturer, i.num_inventory, i.num_inbound , " +
                " i.num_outbound, i.time_latest_inbound, i.time_latest_outbound  " +
                " FROM `test`.`car_accessory` AS c , `test`.`inventory` AS i WHERE c.cid = i.cid";

        if(searchWord != "")
        {
            sql += " AND (";

            sql += " c.cid LIKE '%" + searchWord + "%'";

            sql += " or cname LIKE '%" + searchWord + "%'";

            sql += " or category LIKE '%" + searchWord + "%'";

            sql += " or manufacturer LIKE '%" + searchWord + "%' )";
        }

        System.out.println(sql);

        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Inventory.class));
    }

    public List<Map<String, String>> staticInfo()
    {
        String sql = "SELECT cname, num_inbound, num_outbound FROM test.inventory i , test.car_accessory c where c.cid = i.cid group by cname;";
        List<Map<String, String>> list = jdbcTemplate.query(sql, (resultSet, i) -> {
                    Map<String, String> map = new HashMap<String, String>();
                    map.put("cname", resultSet.getString("cname"));
                    map.put("num_inbound", resultSet.getString("num_inbound"));
                    map.put("num_outbound", resultSet.getString("num_outbound"));
                    return map;
                }
        );
        return list;

    }


}
